
/**
------------------------------------------------------------
* NGUYEN SY BACH
* bit.mta@gmail.com
* Hoc Vien Ky Thuat Quan Su
------------------------------------------------------------
***	source bien doi bang cpu
------------------------------------------------------------
*/


#include "cpu_transfor.h"

void NearestNeighbor_hst(const uchar *inPtr, int strideIn,
	int channels, double x, double y,
	uchar *outPtr, int strideOut, int u, int v)
{
	x = int(x + 0.5);
	y = int(y + 0.5);
	int i = INDEX(int(x), int(y), strideIn, channels);
	int j = INDEX(u, v, strideOut, channels);
	for (int r = 0; r < channels; ++r){
		outPtr[j + r] = inPtr[i + r];
	}
}

void TransformWithCPU(BYTE *&outPtr, int u0, int v0,
	int outWidth, int outHeight, int channels, const double *g,
	const BYTE *inPtr, int x0, int y0,
	int inWidth, int inHeight, int mode)
{
	double x, y;
	if (mode == NEARESTNEIGHBOR){
		for (int v = 0; v < outHeight; ++v){
			for (int u = 0; u < outWidth; ++u){
				x = g[0] * (u0 + u) + g[1] * (v0 + v) + g[2];
				y = g[3] * (u0 + u) + g[4] * (v0 + v) + g[5];
				x -= x0, y -= y0;
				if (x >= 0 && x < inWidth && y >= 0 && y < inHeight){
					NearestNeighbor_hst(inPtr, channels*inWidth, channels, x, y,
						outPtr, channels*inHeight, u, v);
				}
				else{
					int i = INDEX(u, v, channels*outWidth, channels);
					outPtr[i] = outPtr[i + 1] = outPtr[i + 2] = 0;
				}
			}
		}
	}
}